// pages/playsong/playsong.js
const innerAudioContext = wx.createInnerAudioContext()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        songid:null,
        idarr:null,     //id列表
        songs:null,
        privileges:null,
        api: require("../web/api/apiurl"),
        detail:null,
        play:false,
        totaltime:0,
        currenttime:0,
        baifenbi:null,
        viocevalue:1,
        wrods:null,
        showprewords:null,
        shownextwords:null,
        showwords:null,
        searchval:null,//回搜索页
        bangval:null,//回榜单页
        arrIndex:-1     // 播放的在列表的位置
    },
    // 获取歌曲详情
    getdetail(){
        
        wx.request({
          url: this.data.api.songdetail+this.data.songid,
          success:res=>{
            console.log(res.data);
            this.setData({
                privileges:res.data.privileges[0],
                songs:res.data.songs[0],
                totaltime:(res.data.songs[0].dt/1000).toFixed(),
                showwords:res.data.songs[0].al.name
            })
            wx.setNavigationBarTitle({
                title: res.data.songs[0].al.name
              })
            
            wx.setBackgroundColor({
              backgroundColor: 'pink',
            })
            wx.request({
            url: this.data.api.songurl+res.data.privileges[0].id,
            success:res2=>{
                this.setData({
                    songdetail:res2.data.data[0]
                })
                console.log(res2);
                innerAudioContext.src = this.data.songdetail.url;
            }
            })
          }
        })
    },
    // 获取歌词
    getlyric(){
        wx.request({
            url: this.data.api.lyric+this.data.songid,
            success:res=>{

                var arr = res.data.lrc.lyric.trim().split(/[\[\]]/)
                var time = {}
                arr.shift()
                for (let index = 0; index < arr.length; index+=2) {
                    var j = arr[index].split(/[\:\.]/);
                    arr[index]=j[0]*60+j[1]*1;
                    time[arr[index]]=arr[index+1]
                }
                this.setData(
                    {
                        wrods:time
                    }
                )
            }
        })
    },
    play(){
        console.log(this.data.play);
        if(!this.data.play){
            setTimeout(function () {
                console.log(innerAudioContext);
                innerAudioContext.play()
              }, 500)
        }
        else{
            innerAudioContext.pause()
        }
       
        this.setData({
            play:!this.data.play
        })
    },
    getCurrent:function(){
        this.setData({
            currenttime:innerAudioContext.currentTime
        })
    },
    // 进度条
    getpro:function(e){
        innerAudioContext.pause()
        innerAudioContext.seek(this.data.totaltime*((e.detail.value)/100))
        
        setTimeout(function () {
            innerAudioContext.play()
          }, 500)
    },
    // 声音音量
    getvioce:function(e){
        innerAudioContext.volume=e.detail.value;
        this.setData({
            viocevalue:e.detail.value
        })
        
    },
    pre:function(){
        var index =this.data.arrIndex;
        if(index==0)return;
        index = index-1;
        this.setData({
            arrIndex:++this.data.arrIndex,
            songid:this.data.idarr[index],
            currenttime:0
        });
        console.log(index);
        innerAudioContext.pause()
        innerAudioContext.seek(0)
        this.getmsg()
    },
    next:function(){
        var index =this.data.arrIndex;
        index = index+1;
        this.setData({
            arrIndex:++this.data.arrIndex,
            songid:this.data.idarr[index],
            currenttime:0
        });
        console.log(index);
        innerAudioContext.pause()
        innerAudioContext.seek(0)
        this.getmsg()
    },
    getmsg:function(){
        this.setData({
            play:false
        })
        this.getdetail()
        this.getlyric()
        this.Interval()
        innerAudioContext.onTimeUpdate(()=>{
            this.setData({
                currenttime:innerAudioContext.currentTime
            })
        })
        // 循环播放111
        innerAudioContext.onEnded(()=>{
            innerAudioContext.pause()
        innerAudioContext.seek(0)
        
        setTimeout(function () {
            innerAudioContext.play()
          }, 500)
        })
    },
    // 返回上一页
    back(){
        innerAudioContext.pause()
        innerAudioContext.seek(0)
        wx.navigateBack({
            delta: 1
          })
    },
    Interval(){
        setInterval(() => {
            for (const key in this.data.wrods) {
                if(innerAudioContext.currentTime-3<key&&key<innerAudioContext.currentTime){
                    // if()
                    this.setData({
                        showwords:this.data.wrods[key]
                    })
                }
            }
        }, 1000);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // searchval
        console.log(options.idarr);
        var arr = JSON.parse(options.idarr) 
      
        var currentindex = arr.findIndex(item=>item==options.songid)
         console.log(currentindex);
        this.setData({
            songid:options.songid,// 点击的id
            idarr:arr,// 所有的id
            arrIndex:currentindex,// 点击的id 的索引
        });
       
        this.getmsg()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        innerAudioContext.stop()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})